package com.example.certificate.service.impl;

import afu.org.checkerframework.checker.oigj.qual.O;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.certificate.dto.OrderIinsDto;
import com.example.certificate.dto.UpdateStatusDTO;
import com.example.certificate.entity.*;
import com.example.certificate.mapper.CustomerAddrMapper;
import com.example.certificate.mapper.OrderCartMapper;
import com.example.certificate.mapper.OrderFromMapper;
import com.example.certificate.mapper.ProductInfoMapper;
import com.example.certificate.service.OrderFromService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.certificate.utils.JsonUtil;
import com.example.certificate.utils.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hao
 * @since 2022-03-10 14:03:12
 */
@Service
public class OrderFromServiceImpl extends ServiceImpl<OrderFromMapper, OrderFrom> implements OrderFromService {

    @Resource
    private OrderFromMapper fromMapper;

    @Resource
    private OrderCartMapper orderCartMapper;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Override
    public ResultEntity insertOrder(OrderIinsDto dto, User user) {

        if(ObjectUtils.nullSafeEquals(dto.getSource(), "order")){

            List<String> list = new ArrayList<>();

            OrderFrom orderFrom = new OrderFrom();
            BeanUtils.copyProperties(dto,orderFrom);
            orderFrom.setUserId(user.getId());
            ProductInfo productInfo = productInfoMapper.selectById(dto.getDetailDesc());
            productInfo.setPurchaseQuantity(dto.getNums());
            productInfo.setPurchaseTotal(dto.getTotalAmount());

            String jsonString = JsonUtil.Object2JsonString(productInfo);
            list.add(jsonString);

            orderFrom.setDetailDesc(list.toString());
            orderFrom.setBelongerId(productInfo.getUserId());
            orderFrom.setUserId(user.getId());
            orderFrom.setStatus(1); //已付款
            fromMapper.insert(orderFrom);
            return new ResultEntity(orderFrom.getId());
        }

        if(ObjectUtils.nullSafeEquals(dto.getSource(), "orderCar")){

            List<ProductInfo> productInfoList = new ArrayList<>();
            String[] strings = dto.getDetailDesc().split(",");

            for (int i = 0; i< strings.length; i++) {

                OrderCart orderCart = orderCartMapper.selectById(strings[i]);

                ProductInfo productInfo = productInfoMapper.selectById(orderCart.getProductInfoId());
                productInfo.setPurchaseQuantity(orderCart.getProductAmount());
                productInfo.setPurchaseTotal(orderCart.getTotalPrice());
                productInfoList.add(productInfo);
                orderCartMapper.deleteById(orderCart.getId());
            }

            Map<String, List<ProductInfo>> groupBusinessCollection = productInfoList.stream().collect(Collectors.groupingBy(ProductInfo::getUserId));
            for (Map.Entry<String, List<ProductInfo>> entryBusinessItem : groupBusinessCollection.entrySet()) {

                OrderFrom orderFrom = new OrderFrom();
                List<String> list = new ArrayList<>();
                List<ProductInfo> entryUserList = entryBusinessItem.getValue();
                Float totalPrice = 0.0f;

                for (ProductInfo v:
                     entryUserList) {
                    String jsonString = JsonUtil.Object2JsonString(v);
                    totalPrice += v.getPurchaseTotal();
                    list.add(jsonString);
                }

                BeanUtils.copyProperties(dto,orderFrom);
                orderFrom.setBelongerId(entryBusinessItem.getKey());
                orderFrom.setDetailDesc(list.toString());
                orderFrom.setTotalAmount(totalPrice);
                orderFrom.setUserId(user.getId());
                orderFrom.setStatus(1); //已付款
                fromMapper.insert(orderFrom);
            }
        }

        return new ResultEntity();
    }

    @Override
    public ResultEntity updateStus(UpdateStatusDTO dto) {
        OrderFrom orderFrom = fromMapper.selectById(dto.getId());
        orderFrom.setStatus(dto.getStatus());
        fromMapper.updateById(orderFrom);
        return new ResultEntity();
    }
}
